package com.zhentao.minioService.impl;

import com.zhentao.config.MinioConfig;
import com.zhentao.config.MinioPojo;
import com.zhentao.minioService.MinioService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.SneakyThrows;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;

@Service
@EnableConfigurationProperties(MinioPojo.class)
@Import(MinioConfig.class)
public class MinioServiceImpl implements MinioService {
    @Resource
    private MinioPojo minioPojo;
    @Resource
    private MinioClient minioClient;

    @SneakyThrows
    @Override
    public String uploadHtmlImage(String fileName, InputStream inputStream) {
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(minioPojo.getPuck())
                .contentType("image/jpg")
                .stream(inputStream, inputStream.available(), -1)
                .object(fileName)
                .build();
        minioClient.putObject(putObjectArgs);
        String url = minioClient.getObjectUrl(putObjectArgs.bucket(), putObjectArgs.object());
        return url;
    }
}
